home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 5 / MacMania 5.toast / / Tools&Utilities / Plotfoil 3.2 / editfoil.c < prev    next >
C/C++ Source or Header  |  1994-12-12  |  5KB  |  205 lines

  1.  
  2. /*
  3.  * editfoil.c - read in an airfoil and edit camber and thickness.
  4.  *
  5.  * Copyright 1994 Shamim P. Mohamed
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * Author:
  22.  * shamim@math.isu.edu
  23.  * Shamim Mohamed
  24.  * Dept. of Mathematics
  25.  * Idaho State University
  26.  *
  27.  */
  28.  
  29. #include "editfoil.h"
  30. static char copyright[] = "\
  31. Editfoil v1.0, Copyright 1994 Shamim Mohamed. This is free software\n\
  32. and is freely distributable under certain conditions; it coms with\n\
  33. NO WARRANTY. See the file \"COPYING\" for details.\n";
  34.  
  35. static char usage_str[] = "\n\
  36. Usage: %s [-hq] [-c <camber>] [-t <thick>] [-o output-file] [datafile]\n\
  37. \n\
  38. Options:\n\
  39.    -q : Don't print the copyright message\n\
  40.    -h : Print this help\n\
  41. These take arguments:\n\
  42.    -c : Make the camber this value\n\
  43.    -t : Make the thickness this value\n\
  44.    -o : Save the data in this file instead of sending to stdout\n\
  45. (Under MS-DOS, / can be used for options instead of -.)\n";
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49.    int qflag=0, tflag=0, cflag=0;
  50.  
  51.    float t=-1., c=-1.;
  52.    FILE *fp=stdin, *fout=stdout;
  53.    int argp = 1;
  54.    airfoil_data_t foil, newfoil;
  55.    char *fname = "<stdin>", *p;
  56.    
  57.    while(argc > argp) {
  58.       p = argv[argp];
  59.       if(optionchar(*p) && (*(p+1) != 0)) {
  60.          switch (*(p+1)) {
  61.      case 't':
  62.         if(sscanf(p = argv[++argp], "%f", &t) != 1 || t < 0.) {
  63.            fprintf(stderr, "Invalid argument for -t: \"%s\"\n", p);
  64.            break;
  65.         }
  66.         tflag++;
  67.         t /= 100;
  68.         break;
  69.      case 'c':
  70.         if(sscanf(p = argv[++argp], "%f", &c) != 1 || c < 0.) {
  71.            fprintf(stderr, "Invalid argument for -c: \"%s\"\n", p);
  72.            break;
  73.         }
  74.         cflag++;
  75.         c /= 100;
  76.         break;
  77.          case 'o':
  78.             fname = argv[++argp];
  79.             if((fout = fopen(fname, "w")) == NULL) {
  80.                fprintf(stderr, "Could not open \"%s\" for writing!\n", fname);
  81.                fout = stdout;
  82.             }
  83.             break;
  84.          case '-':
  85.             goto done_opts;
  86.          case 'h':
  87.          case '?':
  88.          default:
  89.             fprintf(stderr, usage_str, argv[0], argv[0]);
  90.             exit(1);
  91.          }
  92.       }
  93.       else
  94.          break;
  95.       argp++;
  96.    }
  97.  
  98.  done_opts:
  99.    
  100.    if(!qflag)
  101.       fputs(copyright, stderr);
  102.  
  103.    if(argp < argc)
  104.       if((fp = fopen((fname = argv[argp]), "r")) == NULL) {
  105.      fprintf(stderr, "%s: can't open \"%s\"\n", argv[0], fname);
  106.      exit(1);
  107.       }
  108.    
  109.    if(!read_foil(fp, fname, &foil))
  110.       exit(1);
  111.    
  112.    edit(&newfoil, &foil, t, c);
  113.  
  114.    writefoil(fout, &newfoil, tflag, cflag);
  115.  
  116.    if(fp != stdin)
  117.       fclose(fp);
  118.    if(fout != stdout)
  119.       fclose(fout);
  120.  
  121.    exit(0);
  122.  
  123.    /* well... */
  124.    return 0;
  125. }
  126.  
  127. static void edit(airfoil_data_t *new, airfoil_data_t *old, float t, float c)
  128. {
  129.    point_t other_side[MAXPOINTS];
  130.    int i;
  131.  
  132.    get_otherside(other_side, old->points, old->npoints);
  133.  
  134.    /* find camber */
  135.    old->camber = 0.0;
  136.    for(i = 0; i < old->npoints; i++) {
  137.       double mean;
  138.  
  139.       mean = 0.5 * (old->points[i].y + (double)(other_side[i].y));
  140.       if(mean > old->camber) {
  141.      old->camber = mean;
  142.      old->cam_loc = old->points[i].x;
  143.       }
  144.    }
  145.  
  146.    /* find thickness */
  147.    old->thickness = 0.0;
  148.    for(i = 0; i < old->npoints; i++) {
  149.       double thick;
  150.  
  151.       thick = (old->points[i].y - (other_side[i].y));
  152.       thick = abs(thick);
  153.       if(thick > old->thickness) {
  154.      old->thickness = thick;
  155.      old->th_loc = old->points[i].x;
  156.       }
  157.    }
  158.  
  159.    strcpy(new->title, old->title);
  160.    new->npoints = old->npoints;
  161.    for(i = 0; i < old->npoints; i++) {
  162.       new->points[i].x = old->points[i].x;
  163.       new->points[i].y = old->points[i].y;
  164.    }
  165.    new->miny = old->miny;
  166.    new->maxy = old->maxy;
  167.    
  168.    /* De we need to modify camber? */
  169.    if(c >= 0.) {
  170.       double factor;
  171.       
  172.       if(old->camber < EPSILON) {
  173.      fputs("Cannot modify camber for a symmetrical airfoil!\n", stderr);
  174.      return;
  175.       }
  176.  
  177.       factor = c / old->camber;
  178.       for(i = 0; i < old->npoints; i++) {
  179.      double m = 0.5*(new->points[i].y + other_side[i].y);
  180.      double newm = m * factor;
  181.      new->points[i].y -= m - newm;
  182.      other_side[i].y -= m - newm;
  183.       }
  184.       new->camber = c;
  185.    }
  186.    else
  187.       new->camber = old->camber;
  188.  
  189.    /* Modify thickness? */
  190.    if (t >= 0.) {
  191.       double factor = t / old->thickness;
  192.       for(i = 0; i < new->npoints; i++) {
  193.      double m = 0.5*(new->points[i].y + other_side[i].y);
  194.      double th = (new->points[i].y - other_side[i].y);
  195.      double newt = th * factor;
  196.      new->points[i].y = m + newt/2.;
  197.      other_side[i].y = m - newt/2.;
  198.       }
  199.       new->thickness = t;
  200.    }
  201.    else
  202.       new->thickness = old->thickness;
  203.    
  204. }
  205.